printing: Enable search through locations in printers list
authorMarek Kasik <mkasik@redhat.com>
Thu, 19 Dec 2013 13:52:16 +0000 (14:52 +0100)
committerMarek Kasik <mkasik@redhat.com>
Mon, 27 Jan 2014 15:40:43 +0000 (16:40 +0100)
Add printer_compare() function for comparing printers according
to their names and locations. It is possible to search by multiple
keys separated by space or tabulator using logical conjunction.

Based on patch by William Hua.

https://bugzilla.gnome.org/show_bug.cgi?id=692931

gtk/gtkprintunixdialog.c
gtk/resources/ui/gtkprintunixdialog.ui

index b83b10034addf673fca33d214df102fbcb3c1ab6..0d2569bc03d871d0c670568a07863e9d96025a15 100644 (file)
@@ -212,6 +212,11 @@ static gboolean set_active_printer                 (GtkPrintUnixDialog *dialog,
                                                     const gchar        *printer_name);
 static void redraw_page_layout_preview             (GtkPrintUnixDialog *dialog);
 static void load_print_backends                    (GtkPrintUnixDialog *dialog);
+static gboolean printer_compare                    (GtkTreeModel       *model,
+                                                    gint                column,
+                                                    const gchar        *key,
+                                                    GtkTreeIter        *iter,
+                                                    gpointer            search_data);
 
 /* GtkBuildable */
 static void gtk_print_unix_dialog_buildable_init                    (GtkBuildableIface *iface);
@@ -758,6 +763,9 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
                                         GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
                                         GTK_SORT_ASCENDING);
 
+  gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (priv->printer_treeview),
+                                       printer_compare, NULL, NULL);
+
   gtk_tree_view_column_set_cell_data_func (priv->printer_icon_column,
                                           priv->printer_icon_renderer,
                                           set_cell_sensitivity_func, NULL, NULL);
@@ -2106,6 +2114,85 @@ selected_printer_changed (GtkTreeSelection   *selection,
   g_object_notify ( G_OBJECT(dialog), "selected-printer");
 }
 
+static gboolean
+printer_compare (GtkTreeModel *model,
+                 gint          column,
+                 const gchar  *key,
+                 GtkTreeIter  *iter,
+                 gpointer      search_data)
+{
+  gboolean matches = FALSE;
+
+  if (key != NULL)
+    {
+      gchar  *name = NULL;
+      gchar  *location = NULL;
+      gchar  *casefold_key = NULL;
+      gchar  *casefold_name = NULL;
+      gchar  *casefold_location = NULL;
+      gchar **keys;
+      gchar  *tmp1, *tmp2;
+      gint    i;
+
+      gtk_tree_model_get (model, iter,
+                          PRINTER_LIST_COL_NAME, &name,
+                          PRINTER_LIST_COL_LOCATION, &location,
+                          -1);
+
+      casefold_key = g_utf8_casefold (key, -1);
+
+      if (name != NULL)
+        {
+          casefold_name = g_utf8_casefold (name, -1);
+          g_free (name);
+        }
+
+      if (location != NULL)
+        {
+          casefold_location = g_utf8_casefold (location, -1);
+          g_free (location);
+        }
+
+      if (casefold_name != NULL ||
+          casefold_location != NULL)
+        {
+          keys = g_strsplit_set (casefold_key, " \t", 0);
+          if (keys != NULL)
+            {
+              matches = TRUE;
+
+              for (i = 0; keys[i] != NULL; i++)
+                {
+                  if (keys[i][0] != '\0')
+                    {
+                      tmp1 = tmp2 = NULL;
+
+                      if (casefold_name != NULL)
+                        tmp1 = g_strstr_len (casefold_name, -1, keys[i]);
+
+                      if (casefold_location != NULL)
+                        tmp2 = g_strstr_len (casefold_location, -1, keys[i]);
+
+                      if (tmp1 == NULL && tmp2 == NULL)
+                        {
+                          matches = FALSE;
+                          break;
+                        }
+                    }
+                }
+
+              g_strfreev (keys);
+            }
+        }
+
+      g_free (casefold_location);
+      g_free (casefold_name);
+      g_free (casefold_key);
+    }
+
+  return !matches;
+}
+
 static void
 update_collate_icon (GtkToggleButton    *toggle_button,
                      GtkPrintUnixDialog *dialog)
index 3d2aec7d39c50f50baa01d29eb966f2b6c067921..cf8cbf74c7329a14a580607f219b46875a903760 100644 (file)
@@ -93,7 +93,6 @@
                                 <property name="visible">True</property>
                                 <property name="can_focus">True</property>
                                 <property name="model">printer_list_filter</property>
-                                <property name="search_column">1</property>
                                 <signal name="row-activated" handler="emit_ok_response" swapped="no"/>
                                 <child internal-child="selection">
                                   <object class="GtkTreeSelection" id="treeview-selection1">